package plus.easydo.xxl.job.request;

import cn.hutool.core.text.CharSequenceUtil;
import plus.easydo.xxl.job.exception.XxlJobPlusException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;

/**
 * @author yuzhanfeng
 * @Date 2023/2/16 16:34
 * @Description 工厂类
 */
@Component
public class XxlJobRequestFactory {

    @Autowired
    private Map<String, XxlJobRequest> cache;

    @Value("${xxl.job.plus.requestType:hutool}")
    private String requestType;

    /**
     * 获取实际请求实例
     *
     * @return com.yunli.govaffair.xxl.job.request.XxlJobRequest
     * @author laoyu
     * @date 2023/2/16
     */
    public XxlJobRequest getRequestTarget(){
        XxlJobRequest xxlJobRequest = cache.get(requestType);
        if(CharSequenceUtil.isEmpty(requestType)){
            throw new XxlJobPlusException("requestType不能为空");
        }
        if(Objects.isNull(xxlJobRequest)){
            throw new XxlJobPlusException("未找到类型为"+requestType+"的XxlJobRequest实现类");
        }
        return xxlJobRequest;
    }

}
